import { Node } from "cc";
import Config, { config_get } from "./config/Config";
import Cryptor from "./encrypt/Cryptor";
import { Extension } from "./extension/Extension";
import Http from "./http/Http";
import LocalData from "./localData/LocalData";
import Logger, { LOG_LEVEL } from "./logger/Logger";
import { director } from "cc";
import { AudioManager } from "./audio/AudioManager";

Extension.Init();
export class SGame {
    private static _inited: boolean = false;

    public static get HasInited(): boolean {
        return this._inited;
    }

    public static Init(config: object) {
        if (this._inited) {
            return;
        }

        this._inited = true;

        let node = new Node("SGameManager");
        node.addComponent(AudioManager);
        director.addPersistRootNode(node);

        Config.Init(config);
        Logger.Init(config_get("logger.enable", true), config_get("logger.level", LOG_LEVEL.LOG));
        Cryptor.Init(config_get("cryptor.key", ""), config_get("cryptor.iv", ""));
        LocalData.Init(config_get("localData.enable", true), config_get("localData.key"), config_get("localData.iv"));
        let httpConfig = (key: string, defaultVal: any = undefined) => {
            return config_get("http." + key, defaultVal);
        }

        Http.Init(
            httpConfig("timeout", 10000),
            httpConfig("headers"),
            httpConfig("responseType", "json"),
            httpConfig("postContentType", "application/json")
        );
    }
}
